<template>
  <div class="tinymce-container">
    <editor
      v-model="myValue"
      :init="init"
      :disabled="disabled"
      @onClick="onClick"
    />
  </div>
</template>
<script>
import tinymce from 'tinymce/tinymce'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/icons/default/icons.min.js'
import 'tinymce/themes/silver'
// 编辑器插件plugins
// 更多插件参考：https://www.tiny.cloud/docs/plugins/
import 'tinymce/plugins/image'// 插入上传图片插件
import 'tinymce/plugins/media'// 插入视频插件
import 'tinymce/plugins/table'// 插入表格插件
import 'tinymce/plugins/lists'// 列表插件
import 'tinymce/plugins/wordcount'// 字数统计插件
import axios from 'axios'
import { fileUrl, staticImgUrl } from '@/url'
export default {
  components: {
    Editor
  },
  props: {
    value: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    plugins: {
      type: [String, Array],
      default: 'lists image table wordcount'
    },
    toolbar: {
      type: [String, Array],
      default: 'undo redo |  formatselect | bold italic forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists table | removeformat | image'
    },
    height: {
      type: Number,
      default: 300
    }
  },
  data() {
    return {
      init: {
        language_url: '/tinymce/zh_CN.js', // public目录下
        language: 'zh_CN',
        skin_url: '/tinymce/skins/ui/oxide', // public目录下
        height: this.height,
        plugins: this.plugins, // 父组件传入 或者 填写个默认的插件 要选用什么插件都可以 去官网可以查到
        toolbar: this.toolbar, // 工具栏 我用到的也就是lists image media table wordcount 这些 根据需求而定
        images_upload_handler: (blobInfo, success, failure) => {
          // console.log(blobInfo)
          const formdata = new FormData()
          formdata.append('imgData', blobInfo.base64())
          formdata.append('name', blobInfo.blob().name)
          const param = new FormData() // 创建form对象
          param.append('file', blobInfo.blob()) // 通过append向form对象添加数据
          // console.log(param.get('file')) // FormData私有类对象，访问不到，可以通过get判断值是否传进去
          const config = {
            headers: { 'Content-Type': 'multipart/form-data' }
          }
          axios.post(fileUrl, param, config)
            .then(res => {
              // console.log(res)
              if (res.data && res.data.picture_url) {
                success(staticImgUrl + res.data.picture_url)
              } else {
                failure('图片上传失败,请检查网络')
                return
              }
            })
        }
      },
      myValue: this.value
    }
  },
  watch: {
    value(newValue) {
      this.myValue = newValue
    },
    myValue(newValue) {
      this.$emit('input', newValue)
    }
  },
  mounted() {
    tinymce.init({})
  },
  methods: {
    onClick(e) {
      this.$emit('onClick', e, tinymce)
    }
  }
}

</script>
<style lang="scss" >
.tox{
  &.tox-silver-sink{
   z-index: 12002 !important;
  }
  .tox-dialog{
    z-index: 12005 !important;
  }
  .tox-dialog-wrap{
    z-index: 12003 !important;
  }
  .tox-dialog-wrap__backdrop{
    z-index: 12004 !important;
  }
}
.tinymce-container {
  position: relative;
  line-height: normal;
}
.tinymce-container>>>.mce-fullscreen {
  z-index: 10000;
}
.tinymce-textarea {
  visibility: hidden;
  z-index: -1;
}
.editor-custom-btn-container {
  position: absolute;
  right: 4px;
  top: 4px;
  /*z-index: 2005;*/
}
.fullscreen .editor-custom-btn-container {
  z-index: 10000;
  position: fixed;
}
.editor-upload-btn {
  display: inline-block;
}
.mce-stack-layout-item {
    display: block;
}
/deep/.mce-label {//  /deep/可穿透css
  font-size: 0px;
}
/deep/.mce-stack-layout-item {
  border-width: 0px !important;
}
/deep/.mce-statusbar>.mce-container-body .mce-path {
  font-size: 0px;
}
</style>
